package com.xnfh.equipment.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.xnfh.common.bean.EquipmentToken
import com.xnfh.common.bean.GateWayBean
import com.xnfh.common.bean.WarningBean
import com.xnfh.common.bean.WarningItem
import com.xnfh.common.constant.ArouterPath
import com.xnfh.equipment.R
import com.xnfh.equipment.adapter.EquipmentWarningAdapter
import com.xnfh.equipment.databinding.FragmentEquipmentLayoutBinding
import com.xnfh.equipment.ui.activity.InternetThingsActivity
import com.xnfh.equipment.ui.activity.KcEquipmentActivity
import com.xnfh.equipment.viewmodel.EquipmentViewModel
import com.xnfh.framework.base.BaseFragment
import com.xnfh.framework.base.BaseStateObserver
import com.xnfh.network.constant.SFJ_API_TOKEN
import com.xnfh.network.constant.SFJ_TOKEN_V2

/**
 * @Description:
 * @Author: ziheng
 * @CreateDate: 2023/11/8 14:39
 * @UpdateUser: ziheng
 * @UpdateDate: 2023/11/8 14:39
 * @UpdateRemark: 更新说明
 */
@Route(path = ArouterPath.EQUIPMENT.EQUIPMENT_FRAGMENT)
class EquipmentFragment : BaseFragment<FragmentEquipmentLayoutBinding>(), View.OnClickListener {
    override fun getLayoutID(): Int = R.layout.fragment_equipment_layout


    private var viewModel: EquipmentViewModel? = null
    private var adapter: EquipmentWarningAdapter? = null
    private var gateWayCount = 0

    //设置给adapter的数据
    private var warningList = ArrayList<WarningItem>()
    override fun initView(view: View, savedInstanceState: Bundle?) {
        mBind.equipmentTitle.backIv.visibility = View.GONE
        mBind.equipmentSfj.setOnClickListener(this)
        mBind.equipmentQsxb.setOnClickListener(this)
        mBind.equipmentTitle.headerTitleTxt.text =
            requireContext().getString(R.string.str_equipment)
        mBind.equipmentWarningRecycle.layoutManager = LinearLayoutManager(requireContext())
        adapter = EquipmentWarningAdapter()
        mBind.equipmentWarningRecycle.adapter = adapter
        mBind.equipmentWarningRefresh.autoRefresh()
        mBind.equipmentWarningRefresh.setOnRefreshListener {
            viewModel?.requestKcTokenV2()
        }
    }

    override fun observe() {
        viewModel = EquipmentViewModel()
        //先获取token，然后获取网关，最后通过网关获取预警列表。其中一步出错都要停止刷新
        viewModel?.tokenV2?.observe(this, object : BaseStateObserver<EquipmentToken>(null) {
            override fun getRespDataSuccess(it: EquipmentToken) {
                super.getRespDataSuccess(it)
                SFJ_API_TOKEN = it.apiToken
                SFJ_TOKEN_V2 = it.TokenV2
                //获取所有网关
                viewModel?.requestGetewayList()
            }

            override fun getRespDataError() {
                super.getRespDataError()
                mBind.equipmentWarningRefresh.finishRefresh()
            }
        })
        viewModel?.gateWayList?.observe(this, object : BaseStateObserver<List<GateWayBean>>() {
            override fun getRespDataSuccess(it: List<GateWayBean>) {
                super.getRespDataSuccess(it)
                gateWayCount = it.size
                warningList.clear()
                it.forEach {
                    viewModel?.requestWarningList(it.gatewayid)
                }
            }

            override fun getRespDataError() {
                super.getRespDataError()
                mBind.equipmentWarningRefresh.finishRefresh()
            }
        })
        viewModel?.warningBean?.observe(this, object : BaseStateObserver<WarningBean>() {
            override fun getRespDataSuccess(it: WarningBean) {
                super.getRespDataSuccess(it)
                gateWayCount--
                warningList.addAll(it.Data)
                if (gateWayCount == 0) {
                    adapter?.setNewInstance(warningList)
                }
                mBind.equipmentWarningRefresh.finishRefresh()
            }

            override fun getRespDataError() {
                super.getRespDataError()
                mBind.equipmentWarningRefresh.finishRefresh()
            }
        })
    }

    override fun onClick(v: View?) {
        when (v) {
            mBind.equipmentSfj -> {
                KcEquipmentActivity.start(requireContext())
            }

            mBind.equipmentQsxb -> {
                InternetThingsActivity.start(requireContext())
            }
        }
    }
}